<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use function Hyperf\Config\config;
use function Hyperf\Support\env;
use Hyperf\Di\Annotation\Inject;
use App\Service\HscoinBillService;
use App\Amqp\Producers\DemoProducer;
use Hyperf\Amqp\Producer;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionMethod;

class IndexController extends AbstractController
{

//    private \App\RpcConsumer\CalculatorServiceInterface $rpcCalculator;

    #[Inject]
    private HscoinBillService $billService;
    #[Inject]
    private Producer $producer;

    public function index()
    {


        $c = $this->request->input('c', '');
        $a = $this->request->input('a', '');
        $method = $this->request->getMethod();

        $data = [
            // 输出通用配置文件
            // 'common' => config('common', 'no value'),
            // 输出本模块自己的配置寄文件
            // 'model' => config('model', 'no value'),

            // 调用RPC的 ADD方法
            //  'rpc1-add' => $this->rpcCalculator->add(12,34),
            // 调用RPC的 SUB方法
            // 'rpc1-sub' => $this->rpcCalculator->sub(55,13),
        ];

        // < sux test error code
        switch ($this->request->input('e', '0')) {
            case 1:
                return $this->jsonSuccess();
                break;
            case 2:
                return $this->jsonSuccess(['b' => true, 'c' => 1, 'd' => 2]);
                break;
            case 3:
                return $this->jsonSuccess('xxx成功!');
                break;
            case 4:
                return $this->jsonError(-8, ['user' => '苏翔', 'email' => 'sux@qq.com']);// 建议常量代替-8
                break;
            case 5:
                return $this->jsonError(self::ERROR_CODE_BUSY);
                break;
            case 6:
                return $this->jsonError('xxx错误!');
                break;
            case 9:
                return $this->jsonError();

        }
        // >
        if ($m = $this->request->input('m', '')) {
            $demoProducer = $this->producer->produce(new DemoProducer, true, 5);
            return $this->jsonSuccess($demoProducer);
            // $result = $demoProducer->send();

            // return ['status' => $result->isSuccess() ? 'success' : 'failed', 'message' => $result->getMessage()];
        }


    }


    /**
     * 测试sux file app/Controller/IndexController.php
     * @RequestMapping(path="sxtest", methods="POST")
     */
    public function sxtest(ContainerInterface $container)
    {


        $c = $this->request->input('c', '');
        $a = $this->request->input('a', '');
        if ('.php' == substr($c, -4)) {
            $c = str_replace(['.php', '/','app\\'], ['', '\\','App\\'], $c);
        }

        if (!class_exists($c) || !method_exists($c, $a)) {
            // return ['status' => 'error', 'message' => 'no c'.$c.'::'.$a];
        }

        $instance = $instance = $container->get($c);;
        $reflectionMethod = new ReflectionMethod($c, $a);

        $params = [];
        foreach ($reflectionMethod->getParameters() as $param) {
            $paramName = $param->getName();
            $params[] = $this->request->input($paramName, $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null);
        }

        $result = $reflectionMethod->invokeArgs($instance, $params);

        return $this->jsonSuccess(['result' => $result]) ;
    }

}
